package com.hxzy.springbootrocketconsumer.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxzy.springbootrocketconsumer.dto.UserChargeDTO;
import com.hxzy.springbootrocketconsumer.entity.Credit;
import com.hxzy.springbootrocketconsumer.mapper.CreditMapper;
import lombok.extern.log4j.Log4j2;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * RocketMQ分布式事务 积分增长
 * @author Fan
 * finished
 */
@Log4j2
@Service
@RocketMQMessageListener(topic = "RLT_ACCOUNT_TOPIC",selectorExpression = "charge", consumerGroup = "consumer_charge")
public class MQTXConsumerService implements RocketMQListener<UserChargeDTO> {

    /**
     * creditMapper 继承mybatisplus  BaseMapper
     */
    @Autowired
    private CreditMapper creditMapper;


    /**
     * 设置更新积分 (积分增长)
     * userChargeDTO -> id -> balance
     * val  -->确认
     * @param userChargeDTO
     */
   @Override
    public void onMessage(UserChargeDTO userChargeDTO) {
        //检查已经加过了 QueryWrapper-->mybatisplus的方法
       QueryWrapper<Credit> queryWrapper=new QueryWrapper<>();
       //确认数据库中userid和用户的传入的id
       queryWrapper.eq("user_id",userChargeDTO.getId());
       //查询原来的信用中的所有属性
       //dbCredit 拥有4个属性 dbCredit-->所有属性
       Credit dbCredit=this.creditMapper.selectOne(queryWrapper);
       //积分=原来 +本次冲值金额
       int score= dbCredit.getIntegration() + userChargeDTO.getBalance();
       //设置原来的积分传入参数score
       dbCredit.setIntegration(score);
        //根据id设置信用的所有属性 把参数传入dbCredit
       this.creditMapper.updateById(dbCredit);
        //success
       log.info("积分增加成功,"+ score);
    }
}
